// pages/card/charge/index.js
const app = getApp()
import http from '../../../utils/request.js';
import alert from '../../../utils/alert.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [],
    cardNo: '',
    orderNo:'',
    scrollHeight:"500px",
    checkItemId: 0,
    chargeBtn: true,
    qrPopupvisible:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var res = wx.getSystemInfoSync();
    this.setData({
      cardNo: options.card_no,
      scrollHeight: res.windowHeight - 118 + 'px'
    })
    this.getPackLists()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.radioItems.length)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getPackLists:function(){
    http.get(app.config.service.getCardPack, { app_id: app.globalData.app_id }).then(data => {
      console.log(data)
      this.setData({
        radioItems: data
      })
    }, err => {
      alert.alert(err)
    });
  },
  radioChange:function(e){
    var radioItems = this.data.radioItems;

    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = false;
    }
    radioItems[e.detail.value].checked = true;
    console.log(radioItems[e.detail.value].id);
    this.setData({
      radioItems: radioItems,
      checkItemId: radioItems[e.detail.value].id
    });
  },
  goPay: function (e) {
    if (!(this.data.cardNo && this.data.checkItemId)){
      alert.alert('卡号或者套餐ID缺失');
      return false;
    }

    http.post(app.config.service.getChargeInfo, { 
      app_id: app.globalData.app_id, 
      pack_id: this.data.checkItemId, 
      card_no: this.data.cardNo,
      pay_type: e.currentTarget.dataset.type,
      pay_channel: e.currentTarget.dataset.channel 
      }).then(data => {
        this.setData({
          orderNo: data.order_no
        })
        if (e.currentTarget.dataset.channel=='scan'){
          if (e.currentTarget.dataset.type=='wechat'){
            var qrCode = data.code_url
          }else{
            var qrCode = data.qr_code
          }
          this.setData({
            qrPopupvisible:true,
            qrValue: qrCode
          })
        }else{
          var that = this;
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success: function (res) {
              that.queryResult(data.order_no)
            },
            error: function (res) {
              alert.alert(res)
            }
          })
        }
    }, err => {
      alert.alert(err)
    });
  },

  queryResult: function (order_no){
    http.post(app.config.service.getChargeResult, {
      order_no: order_no,
    }).then(data => {
      var str =data.desc;
      if(data.is_to){
        str+=";已经到账";
      }else{
        str += ";未到账";
      }
      if(data.pay_ok==0){
        alert.alert(str)
      }else{
        alert.toast(str);
      }
    }, err => {
      alert.alert(err)
    });
  },
  queryPayResult: function (e) {
    this.queryResult(this.data.orderNo);
  },
  onQrClose: function (e) {
    this.queryResult(this.data.orderNo);
    this.setData({
      qrPopupvisible: false
    })
  },
})